import React from 'react';
import { IMember } from './../service';
import { ServiceTabKey, TabKey } from './consts';
import IconDepartment from '../assets/imgs/ic-department';
import IconPosition from '../assets/imgs/ic-position';
import IconGroup from '../assets/imgs/ic-group';
import IconGroupDisabled from '../assets/imgs/ic-group-disabled';
import IconUser from '../assets/imgs/ic-user';

export const genKey = (tabKey: string, data: IMember) => {
  return `${tabKey}-${data.identityid}`;
};

export const decorateKey =
  (tabKey: string, extra?: (data: IMember) => Record<string, any>) => (data: IMember) => {
    return {
      ...data,
      ...extra?.(data),
      title: data.name,
    };
  };

export const getTabKeyByData = (data: IMember) => {
  if (data?.memberarea == 5 || data?.MemberArea == 5) {
    return 'department';
  } else if (data?.memberarea == 4 || data?.MemberArea == 4) {
    return 'position';
  } else if (data?.memberarea == 3 || data?.MemberArea == 3) {
    return 'group';
  } else if (data?.memberarea == 0 || data?.MemberArea == 0) {
    return 'user';
  } else {
    return '';
  }
};

export const decorateRowKey = (tabKey: string) => (data: Record<string, any>) =>
  `${tabKey}-${data.identityid}`;

export const calcTableScrollHeight = (height: number, pagination: boolean) => {
  return height - 43 - 52 - (pagination ? 34 : 0);
};

export const getMemberIconByKey = (key: TabKey, disabled: Boolean = false) => {
  switch (key) {
    case 'department':
      return React.createElement(IconDepartment);
    case 'position':
      return React.createElement(IconPosition);
    case 'group':
      return disabled ? React.createElement(IconGroupDisabled) : React.createElement(IconGroup);
    case 'user':
      return React.createElement(IconUser);
    default:
      return null;
  }
};

export const getServiceTabKey = (item: { memberKey: TabKey }) => {
  switch (item?.memberKey) {
    case 'department':
      return ServiceTabKey.Department;
    case 'position':
      return ServiceTabKey.Position;
    case 'group':
      return ServiceTabKey.Group;
    case 'user':
      return ServiceTabKey.User;
    default:
      return '';
  }
};

export const getLowercase = (data: any[], tabKey?: string) => {
  return data.map((item) => {
    let _obj = {};
    for (const key in item) {
      _obj[key.toLowerCase()] = item[key];
    }
    if (tabKey) {
      _obj['key'] = `${tabKey}-${_obj?.['identityid']}`;
    }
    return _obj;
  });
};
